import DBMgr from "../db/DBMgr";

export default class SoundMgr {
    protected static _bgmaudioID:number;
    protected static _bgm:string;
    private static _enabledBgm: boolean = true;
    private static _enabledEffect: boolean = true;
    private static _maxEffectNum:number = 5;
    private static _effectSoundDict : Map<number,number> = new Map<number,number>();
    constructor(){
        
    }

    public static mainBGM()
    {
        this.playBGM(DBMgr.data.soundDB[2000001]);
    }

    public static fightBGM()
    {
        this.playBGM(DBMgr.data.soundDB[101]);
    }

    public static btn()
    {
        this.playSound(DBMgr.data.soundDB[2000003]);
    }

    public static playSoundById(id: number) {
        if (!id) return;
        this.playSound(DBMgr.data.soundDB[id]);
    }

    /**
     * 播放背景音乐
     */
    private static playBGM(data:any)
    {
        const url = data['soundMp3'];
        if (this._bgm == url ) { // 同样的背景音乐
            return;
        }
        this._bgm = url;
        console.log("playBGM",url,this._bgmaudioID);
        if(this._bgmaudioID!=undefined)
        {
            cc.audioEngine.stopMusic();
            this._bgmaudioID = undefined;
        }
        cc.resources.load("sound/"+url,cc.AudioClip,function(err, clip){
            if(err||this._bgm!=url)
            {
                return;
            }
            if(this._enabledBgm)
            {
                const volume = Number(data['volume']);
                if(volume){
                    cc.audioEngine.setMusicVolume(volume);
                }
                this._bgmaudioID = cc.audioEngine.playMusic(clip, true);
                if (!this._bgmaudioID) {
                    this._bgmaudioID = 1;
                }
            }
        }.bind(this));
    }

    /**
     * 播放音效
     */
     private static playSound(data:any)
    {
        if(!this._enabledEffect)return;
        const url = data['soundMp3'];
        if (!url)return;
        if(SoundMgr._effectSoundDict[data.id] == null)SoundMgr._effectSoundDict[data.id] = 0;
        if(SoundMgr._effectSoundDict[data.id] > SoundMgr._maxEffectNum){
            console.log("同一个音效最大数量上限")
            return;
        }
        SoundMgr._effectSoundDict[data.id]++;
        cc.resources.load("sound/"+url,cc.AudioClip,function(err, clip){
            if(err)
            {
                cc.log("err",url,err)
                return;
            }
            const volume = Number(data['volume']);
            let audioId = cc.audioEngine.playEffect(clip,false);
            cc.audioEngine.setFinishCallback(audioId,()=>{
                SoundMgr._effectSoundDict[data.id]--;
                // console.log("播放完毕："+data['soundMp3']+"当前音效数:"+SoundMgr._effectSoundDict[data.id]);
            })
            
            if (volume) {
                cc.audioEngine.setVolume(audioId,volume)
                // cc.audioEngine.setEffectsVolume(volume);
            } else {
                cc.audioEngine.setVolume(audioId,1)
                // cc.audioEngine.setEffectsVolume(1);
            }
        }.bind(this));
        return;
    }

    public static pauseAll(){
        console.log("pauseAll!");
        this.enabledBgm = false;
        this.enabledEffect = false;
    }
    
    public static resumeAll(){
        console.log("resumeAll!");
        this.enabledBgm = true;
        this.enabledEffect = true;
    }

    public static stopBGM() {
        if(this._bgmaudioID != undefined)
        {
            cc.audioEngine.stopMusic();
            this._bgmaudioID = undefined;
            this._bgm = null;
        }
    }

    protected static saveSetting()
    {
        try{
            cc.sys.localStorage.setItem("escapesoundset",(this.enabledBgm?1:0)+":"+(this.enabledEffect?1:0));
        }catch(err)
        {
            cc.log(err);
        }
    }

    public static get enabledBgm(): boolean {
        return this._enabledBgm;
    }

    public static set enabledBgm(value: boolean) {
        this._enabledBgm = value;
        if(this._enabledBgm)
        {
            this.playBGM(this._bgm)
        }else
        {
            if(this._bgmaudioID!=undefined)
            {
                cc.audioEngine.stopMusic();
                this._bgmaudioID = undefined;
            }
            this._bgmaudioID = undefined;
        }
    }

    public static get enabledEffect(): boolean {
        return this._enabledEffect;
    }

    public static set enabledEffect(value: boolean) {
        this._enabledEffect = value;
    }

}